package convert;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class XMLConvert1 {
	private String originXMLURL = "http://120.27.29.201:25022/QDATA?TIME=2016-01-01%2012:12:12&COUNT=4";
	private int monitorNum = 5;
	private List<MonitorPoint> monitor1 = new ArrayList<MonitorPoint>();
	private List<MonitorPoint> monitor2 = new ArrayList<MonitorPoint>();
	private List<MonitorPoint> monitor3 = new ArrayList<MonitorPoint>();
	private List<MonitorPoint> monitor4 = new ArrayList<MonitorPoint>();
	private List<MonitorPoint> monitor5 = new ArrayList<MonitorPoint>();

	public XMLConvert1() {

	}

	public void setURL(String url) {
		this.originXMLURL = url;
	}

	public String getStartTime() {
		return ",,,,";
	}

	public String getStopTime() {
		return ",,,,";
	}

	public String getFusionChartsXML() {
		return "";
	}

	public int getCount() {
		String[] temp = originXMLURL.split("COUNT=");
		return Integer.parseInt(temp[1]);
	}

	public static void main(String[] args) throws Exception {
		XMLConvert1 test = new XMLConvert1();
		System.out.println(test.getCount() + "");
		test.run();
	}

	public void run() throws Exception {
		// 鐢熸垚涓�釜URL瀵硅薄
		URL url = new URL(originXMLURL);
		// 鎵撳紑URL
		HttpURLConnection urlConnection = (HttpURLConnection) url
				.openConnection();
		// 寰楀埌杈撳叆娴侊紝鍗宠幏寰椾簡缃戦〉鐨勫唴瀹�
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				urlConnection.getInputStream()));
		String content = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
		String line;
		// 璇诲彇杈撳叆娴佺殑鏁版嵁锛屽苟鏄剧ず
		while ((line = reader.readLine()) != null) {
			// System.out.println(line);
			content = content + "\n" + line;
		}
		System.out.println(content);
		// =====================================================================
		// new ByteArrayInputStream(content.getBytes());
		try {
			parse(new ByteArrayInputStream(content.getBytes()));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void parse(InputStream is) throws ParserConfigurationException,
			SAXException, IOException {
		SAXParserFactory saxFactory = SAXParserFactory.newInstance();
		SAXParser parser = saxFactory.newSAXParser();
		MyHandler handler = new MyHandler();
		parser.parse(is, handler);

	}

	private class MyHandler extends DefaultHandler {
		private List<MonitorPoint> monitor1 = new ArrayList<MonitorPoint>();
		private List<MonitorPoint> monitor2 = new ArrayList<MonitorPoint>();
		private List<MonitorPoint> monitor3 = new ArrayList<MonitorPoint>();
		private List<MonitorPoint> monitor4 = new ArrayList<MonitorPoint>();
		private List<MonitorPoint> monitor5 = new ArrayList<MonitorPoint>();
		private MonitorPoint monitorPoint;
//		private String 
		private String preTag;

		@Override
		public void startDocument() throws SAXException {
			// TODO Auto-generated method stub
			super.startDocument();
		}

		@Override
		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
			super.startElement(uri, localName, qName, attributes);
			preTag = qName;
			if (XMLConst.Timestamp.equals(qName)) {
				System.out.println(attributes.getValue(0));
			} else if (XMLConst.F1_Settlement.equals(qName)) {
			} else if (XMLConst.F2_Settlement.equals(qName)) {

			} else if (XMLConst.F3_Settlement.equals(qName)) {

			} else if (XMLConst.F4_Settlement.equals(qName)) {

			} else if (XMLConst.F5_Settlement.equals(qName)) {
			}
		}

		@Override
		public void characters(char[] ch, int start, int length)
				throws SAXException {
			super.characters(ch, start, length);
			if (preTag != null) {
				String content = new String(ch, start, length);
				if ("name".equals(preTag)) {
//					book.setName(content);
				} else if ("price".equals(preTag)) {
//					book.setPrice(Float.parseFloat(content));
				}
				if (XMLConst.F1_Settlement.equals(preTag)) {
					
				} else if (XMLConst.F2_Settlement.equals(preTag)) {

				} else if (XMLConst.F3_Settlement.equals(preTag)) {

				} else if (XMLConst.F4_Settlement.equals(preTag)) {

				} else if (XMLConst.F5_Settlement.equals(preTag)) {
				}
			}
		}

	}
}
